package com.sixmac.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.annotation.Json;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.ClientUser;
import com.sixmac.service.ClientUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * ClientUserController
 * 2018-09-29 16:50:13
 *
 * @author Blade
 */
@Controller
@RequestMapping("/installClientUser")
public class InstallClientUserController extends BaseController {
    private static String CODE = "installClientUser";
    private static String PREFIX = "room_user";
    private static String LIST_SOURCE = "installClientUser.list";
    private static String BASE_PATH = "/platform/installClientUser/";

    @Autowired
    ClientUserService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "clientUser.html";
    }

    @Json
    @RequestMapping("/ban")
    public AjaxResult ban() {
        String ids = getParameter("ids");
        CMap updateMap = CMap.init().set("ids", Convert.toIntArray(ids));
        boolean temp = service.updateBy("status = (CASE WHEN STATUS=2 THEN 3 ELSE 2 END)", "id in (#{join(ids)})", updateMap);
        if (temp) {
            return success("操作成功!");
        } else {
            return error("操作失败!");
        }
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "clientUser_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        ClientUser clientUser = service.findById(id);
        mm.put("model", JsonKit.toJson(clientUser));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "clientUser_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        ClientUser clientUser = service.findById(id);
        mm.put("model", JsonKit.toJson(clientUser));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "clientUser_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Object grid = paginate(LIST_SOURCE);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        ClientUser clientUser = mapping(PREFIX, ClientUser.class);
        clientUser.setType(4);//安装人员
        clientUser.setTime(new Date());
        boolean temp = service.save(clientUser);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        ClientUser clientUser = mapping(PREFIX, ClientUser.class);
        boolean temp = service.update(clientUser);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }
}
